package com.be.beadmin.service.controller;

import com.be.beadmin.service.dto.output.NotifyInfoEx;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.service.service.NotifyInfoService;
import com.be.beadmin.service.dto.input.NotifyInfoDto;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;

/**
* @author ljh
* @date 2023-09-20
**/
@RestController
public class NotifyInfoController {

    @Autowired
    private  NotifyInfoService notifyInfoService;


    @PostMapping(value = "/service/getNotifyInfoByPage")
    public ResultModel getNotifyInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(notifyInfoService.getNotifyInfoByPage(pageEntity)) ;
    }

    @PostMapping("/service/addNotifyInfo")
    public ResultModel addNotifyInfo(@RequestBody NotifyInfoDto notifyInfoDto){
        notifyInfoService.addNotifyInfo(notifyInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/service/delNotifyInfo")
    public ResultModel delNotifyInfo(@RequestBody NotifyInfoDto notifyInfoDto){
        notifyInfoService.delNotifyInfo(notifyInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/service/updateNotifyInfo")
    public ResultModel updateNotifyInfo(@RequestBody NotifyInfoDto notifyInfoDto){
        notifyInfoService.updateNotifyInfo(notifyInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/service/exportNotifyInfo")
    public void exportNotifyInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<NotifyInfoEx> notifyInfoPage = notifyInfoService.getNotifyInfoByPage(pageEntity);
        List<NotifyInfoEx> notifyInfoExes = notifyInfoPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("NotifyInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), NotifyInfoEx.class).sheet("sheet1").doWrite(notifyInfoExes);
    }
}